package org.nch.videoLauncher.view;

import org.nch.videoLauncher.domain.PlaybackEvent;
import org.nch.videoLauncher.presenter.HistoryPresenter;

import javax.swing.*;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.table.AbstractTableModel;
import java.util.List;
import java.text.SimpleDateFormat;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

/**
 * Created by IntelliJ IDEA.
 * User: bill
 * Date: Jan 8, 2009
 * Time: 12:48:57 PM
 * To change this template use File | Settings | File Templates.
 */
public class HistoryView implements IContentPanel, IHistoryView {
    private JPanel mainPanel;
    private JTable historyTable;
    private JPanel buttonPanel;
    private JButton playButton;

    private List<PlaybackEvent> playbackEvents;
    private Integer currentRow;
    private tableModel historyTableModel;
    private HistoryPresenter presenter;
    private Integer currentTableRow;

    public HistoryView() {
        playButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                presenter.playRequested();
            }
        });
    }

    public JPanel getRootComponent() {
        return mainPanel;
    }

    public void setPresenter(HistoryPresenter presenter) {
        this.presenter = presenter;
    }

    public void setPlaybackEvents(List<PlaybackEvent> playbackEvents) {
        this.playbackEvents = playbackEvents;
        historyTableModel = new tableModel();
        historyTable.setModel(historyTableModel);
        historyTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        historyTable.getSelectionModel().addListSelectionListener(new SelectionListener(historyTable));
    }

    public Integer getSelectionIndex() {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }

    public void setPlayButtonEnabled(boolean enabled) {
        playButton.setEnabled(enabled);
    }

    private class tableModel extends AbstractTableModel {

            public int getRowCount(){ return playbackEvents.size();}
            public int getColumnCount(){ return 3;}

            public String getColumnName(int col){    // TODO:  need a cleaner implementation of the table column names
                String headings[] = new String[]{"Playback Date & Time", "Playback Profile", "Video Title"};
                return headings[col];
            }

            public Object getValueAt(int row, int col) {
                if (col == 0) {
                    SimpleDateFormat formatter = new SimpleDateFormat("EEE MMM d yyyy, H:mm");

                    return formatter.format(playbackEvents.get(row).getDate());
                }
                if (col == 1)
                    return playbackEvents.get(row).getProfile().getName();
                if (col == 2)
                    return playbackEvents.get(row).getVideoItem().getTitle();
                else
                    return "";
            }
    };


    private class SelectionListener implements ListSelectionListener {

        JTable table;

        SelectionListener(JTable table){
            this.table = table;
        }

        public void valueChanged(ListSelectionEvent event) {

            if (!event.getValueIsAdjusting()){

                if (table.getSelectedRow() != -1){
                    presenter.selectionChanged(table.getSelectedRow());
                    currentTableRow = table.getSelectedRow();
                }
            }
        }
    }

}
